import 'package:flutter/material.dart';
import 'package:interview/app/app.dart';
import 'package:interview/app/app_init.dart';
import 'package:interview/providers/app_refresh_providers.dart';
import 'package:interview/providers/theme_providers.dart';
import 'package:interview/utils/startup_monitor.dart';
import 'package:provider/provider.dart';

void main() async {
  final startupMonitor = StartupMonitor();
  startupMonitor.mark("mainStart");
  WidgetsFlutterBinding.ensureInitialized(); // Flutter 框架初始化
  startupMonitor.mark("Flutter-Initialized");

  // 初始化配置
  final appConfig = await appInit();
  startupMonitor.mark("appConfig-Initialized");
  // 创建 ThemeProvider 实例
  final themeProvider = ThemeProvider();
  // 创建 AppRefreshProvider 实例
  final appRefreshProvider = AppRefreshProvider();
  await themeProvider.initializeTheme(); // 创建实例时 就要调用init 初始化 
  startupMonitor.mark("themeProvider-Initialized");
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => themeProvider), // 注册 ThemeProvider
        ChangeNotifierProvider(create: (_) => appRefreshProvider)  // AppRefreshProvider
      ],
      child: MyApp(config: appConfig),
    ),
  );
  WidgetsBinding.instance.addPostFrameCallback((_) {
    startupMonitor.mark("firstFrameRendered");
    startupMonitor.logResults();
  });
}

